package com.coscon.service.patternCommon.impl.systemos;

import com.coscon.model.IrcaCmd;
import com.coscon.service.patternCommon.ICommand;
import lombok.extern.slf4j.Slf4j;

import javax.websocket.Session;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.IOException;

/**
 * 系统控制：锁定系统屏幕
 */
@Slf4j
public class SystemOsLockCmdImpl implements ICommand {

    private Robot rb;

    public SystemOsLockCmdImpl(Robot rb){
        this.rb = rb;
    }


    @Override
    public Boolean executeByMac(IrcaCmd cmd, Session session) {
        lockByMac();
        return true;
    }

    @Override
    public Boolean executeByWin(IrcaCmd cmd, Session session) {
        //第一种方式
        this.lockScreenByWin();
//        //第二种
//        rb.keyPress(KeyEvent.VK_WINDOWS);
//        rb.delay(10);
//        rb.keyPress(KeyEvent.VK_L);
//        rb.delay(10);
//        rb.keyRelease(KeyEvent.VK_L);
//        rb.delay(50);
//        rb.keyRelease(KeyEvent.VK_WINDOWS);

        return true;
    }

    @Override
    public Boolean executeByLinux(IrcaCmd cmd, Session session) {
        return true;
    }



    private void lockByMac(){
        //锁定屏幕，测试通过
        rb.keyPress(KeyEvent.VK_CONTROL);
        rb.keyPress(KeyEvent.VK_META);
        rb.keyPress(KeyEvent.VK_Q);
        rb.delay(50);
        rb.keyRelease(KeyEvent.VK_CONTROL);
        rb.keyRelease(KeyEvent.VK_META);
        rb.keyRelease(KeyEvent.VK_Q);
    }

    //锁定屏幕
    private void lockScreenByWin(){
        try {
            Runtime.getRuntime().exec("cmd /c %windir%\\System32\\rundll32.exe user32.dll,LockWorkStation");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
        }
    }
}
